package model.events

import model.postgresql.{PgBaseEvent, PgLikeRemove}
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}
import zio.json.*

import java.time.ZonedDateTime

case class LikeRemove(
    `object`: LikeRemove.`object`,
    event_id: Option[String],
    timestamp: Option[Long]
) extends BaseEvent(event_id, timestamp) {
  override def convertToJson: String              = this.toJson
  override def getUserId: Some[Long]              = Some(this.`object`.liker_id)
  override def setTimestamp(ts: Long): LikeRemove = copy(timestamp = Some(ts))

  override def toPg: Option[PgBaseEvent] =
    for {
      eventId   <- event_id
      timestamp <- timestamp
    } yield PgLikeRemove(
      eventId,
      `object`.object_type,
      `object`.object_id,
      timestamp,
      `object`.liker_id
    )
}

object LikeRemove {

  case class `object`(
      liker_id: Long,
      object_type: String,
      object_owner_id: Long,
      object_id: Long,
      thread_reply_id: Long,
      post_id: Long
  )

  given JsonDecoder[`object`] = DeriveJsonDecoder.gen[`object`]
  given JsonEncoder[`object`] = DeriveJsonEncoder.gen[`object`]

  given JsonDecoder[LikeRemove] = DeriveJsonDecoder.gen[LikeRemove]
  given JsonEncoder[LikeRemove] = DeriveJsonEncoder.gen[LikeRemove]
}
